/*******************************************************************************
 * Copyright 2011 @ Kapil Viren Ahuja
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.kapilvirenahuja.eamsteps.parser;

import java.util.Map;

import com.kapilvirenahuja.eamsteps.common.DataProviderException;
import com.kapilvirenahuja.eamsteps.dataloader.IDataLoader;
import com.kapilvirenahuja.eamsteps.dataprovider.impl.DefaultDataProvider;
import com.kapilvirenahuja.eamsteps.form.IDataForm;
import com.kapilvirenahuja.eamsteps.form.impl.BaseDataForm;


/**
 * <p>
 * Provides an implementation of {@link IDataLoader} loading data provided by {@link DefaultDataProvider} implementation.
 * </p>
 * 
 * <p>
 * This loader will load up the data in a Java POJO that has been setup with a listing of all the listings for an Excel
 * file. If the {@link IDataForm} and excel are not setup correctly, this loader will raise an
 * {@link DataProviderException}.
 * </p>
 * 
 * <p>
 * Refer to Excel Template for setting up the Excel sheet. For the Java object representing the {@link IDataForm},
 * please remember a few things that are absolutely mandatory. If the objects are not setup correctly, this loader will
 * not work. Rules:
 * <ol>
 * <li>Loader represents a sheet in the excel workbook.</li>
 * <li>Every Form should extend from {@link BaseDataForm}. This will by default add support to store data in a
 * {@link Map} and also store responses.</li>
 * <li>This loader supports only a handful of native data types - <code>String, Date, Boolean, Integer</code>. We will
 * add more support later.</li>
 * </ol>
 * </p>
 * 
 * <p>
 * As of now the only way to extend this loader is by extending this and overriding the method. However, in future we
 * will provider support with injection capabilities so you can write your own components and change the functionality
 * as desired.
 * </p>
 * 
 */
public class ExcelDataLoader
{

    // private void setupResponse(String rowId, String data, HeaderInfo header)
    // {
    // logger.debug("Loading response ## for row ##", new String[] { data, rowId });
    // String responseReference = StringUtils.substringBefore(data, " [");
    // StringBuffer key = new
    // StringBuffer(tableName).append('-').append(responseReference).append('[').append(rowId).append(']');
    //
    // Method adder;
    // try
    // {
    // adder = ReflectionUtil.findAdder(this.form.getClass(), header.getPropertyName());
    // }
    // catch (Exception ex)
    // {
    // throw new DataProviderException("Unable to load the adder " + header.getPropertyName() + "] on the form.", ex);
    // }
    //
    // // Loading the value from an existing reference.
    // Object value = loadReference(data);
    //
    // if (value == null)
    // {
    // throw new DataProviderException("There is a missing Response from the existing references. Please check [" + data
    // + "] on the form.");
    // }
    //
    // try
    // {
    // // Execute the adder to set the value as read from the reference.
    // adder.invoke(form, key.toString().toUpperCase(), value);
    // }
    // catch (Exception ex)
    // {
    // throw new DataProviderException("Error Executing adder and setting up the response for the request [" + key +
    // "]", ex);
    // }
    // }




    // private void setupReferenceArrays(String rowId, String data, HeaderInfo header)
    // {
    // logger.debug("Loading reference arrays ## for row ##", new String[] { data, rowId });
    // List<String> referenceDataRows = loadReferenceDataListing(rowId, data);
    //
    // if (referenceDataRows == null)
    // {
    // logger.warn("No references located for row", new String[] { rowId });
    // }
    // else
    // {
    // for (String referenceDataRow : referenceDataRows)
    // {
    // // Format data such that setupReference can read it.
    // this.setupReference(rowId, referenceDataRow, header);
    // }
    // }
    // }




//    private List<String> loadReferenceDataListing(String rowId, String data)
//    {
//        String referenceObject = StringUtils.substringBefore(data, " [");
//        Integer firstRowNumber = Integer.parseInt(StringUtils.substringBetween(data, "[", "..."));
//        Integer lastRowNumber = Integer.parseInt(StringUtils.substringBetween(data, "...", "]"));
//
//        List<String> rows = new ArrayList<String>();
//        for (Integer rowNumber = firstRowNumber; rowNumber <= lastRowNumber; rowNumber++)
//        {
//            // Reformat Row data for individual references.
//            StringBuffer reformat = new StringBuffer(referenceObject); // NOPMD
//            reformat.append(" [").append(rowNumber).append(']');
//            rows.add(reformat.toString());
//        }
//
//        return rows;
//    }
//



}
